import pymysql
import xlwt

def export_excel(table_name):
    # 连接数据库
    host,user,passwd,db='127.0.0.1','root','xuehaoning','game'
    conn = pymysql.connect(host=host, user=user, password=passwd, database=db,charset="utf8")
    cur = conn.cursor()
    sql = 'select * from %s'%table_name
    cur.execute(sql) # 返回受影响的行数
    
    fields = [field[0] for field in cur.description] # 获取所有字段名
    all_data = cur.fetchall()
    
    #写入excel
    book = xlwt.Workbook()
    sheet = book.add_sheet('sheet1')

    for col,field in enumerate(fields):
        sheet.write(0,col,field)
    row = 1
    for data in all_data:
        for col,field in enumerate(data):
            sheet.write(row,col,field)
        row += 1
    book.save("%s.xls"%table_name)

if __name__=='__main__':
    export_excel('userinfo')
